/*
    Copyright 2006 Todi.
    Send feedback and suggestions to todi@peerfeeds.com.  
  
    This file is part of Peerfeeds.

    Peerfeeds is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    Peerfeeds is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Peerfeeds; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA 
  
*/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;

namespace Peerfeeds
{
    public partial class Import : Form
    {
        private ArrayList feeds = new ArrayList();

        public Import()
        {
            InitializeComponent();
        }

        // define delegate
        public delegate void PassControl(object sender);

        // create instance (null)
        public PassControl passControl;

        private void btnImportOpenFile_Click(object sender, EventArgs e)
        {
            DialogResult dr = importOpenFileDialog.ShowDialog();

            if (dr == DialogResult.OK && !String.IsNullOrEmpty(importOpenFileDialog.FileName))
            {
                txtImportFile.Text = importOpenFileDialog.FileName;
                ImportFeeds(importOpenFileDialog.FileName);
            }
        }

        private void ImportFeeds(string path)
        {
            Xml xml = new Xml();

            feeds = xml.ReadFeedsFromXML(path);

            chlImportFeedList.Items.Clear();

            foreach (Xml.Feed feed in feeds)
            {
                chlImportFeedList.Items.Add(feed.textCommand + " - " + feed.url, true);
            }

            lblImportFeedsLoaded.Text = feeds.Count.ToString() + " feeds loaded";
        }

        private void btnImportRun_Click(object sender, EventArgs e)
        {
            ArrayList eFeeds = new ArrayList();

            // First show the index and check state of all selected items.
            foreach (int indexChecked in chlImportFeedList.CheckedIndices)
            {
                eFeeds.Add(feeds[indexChecked]);
            }

            if (eFeeds.Count > 0)
            {
                passControl(eFeeds);
            }

            this.Close();
        }

        private void btnImportCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void chlImportFeedList_ItemCheck(object sender, ItemCheckEventArgs e)
        {
            btnImportRun.Enabled = true;

            if (e.NewValue == CheckState.Checked)
            {
                btnImportRun.Text = "(" + (chlImportFeedList.CheckedItems.Count + 1) + ") Import";
            }
            else if (chlImportFeedList.CheckedItems.Count > 1)
            {
                btnImportRun.Text = "(" + (chlImportFeedList.CheckedItems.Count - 1) + ") Import";
            }
            else
            {
                btnImportRun.Text = "Import";
                btnImportRun.Enabled = false;
            }

            //if (chlImportFeedList.CheckedItems.Count > 0)
            //{
            //    btnImportRun.Text = "(" + (chlImportFeedList.CheckedItems.Count) + ") Import";
            //}
            //else
            //{
            //    btnImportRun.Text = "Import";
            //}
        }
    }
}